<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\AudioApi;


class MoonGift extends Base
{
    protected $table = 'moon_gift_log';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }


    public function create($user_id, $num)
    {
        $data['user_id'] = $user_id ?? 1;
        $data['ticket_num'] = $num ?? 1;
        return DB::table($this->table)->insert($data);
    }

    public function getNum($user_id)
    {
        $where['user_id'] = $user_id;
        $res = DB::table($this->table)->where($where)->first();
        if (!$res) {
            return [];
        }
        return $res;
    }


    //增
    public function incrementNum($user_id, $num = 0)
    {
        $where['user_id'] = $user_id;
        return DB::table($this->table)->where($where)->increment('ticket_num', $num);
    }

    //减
    public function decrementNum($user_id, $num = 0)
    {
        $where['user_id'] = $user_id;
        DB::table($this->table)->where($where)->increment('consumer_num', $num);
        return DB::table($this->table)->where($where)->decrement('ticket_num', $num);
    }

    public function getAllTicket()
    {
        $data['ticket_sum'] = DB::table($this->table)->sum('ticket_num') ?? 0;
        $data['consumer_sum'] = DB::table($this->table)->sum('consumer_num') ?? 0;
        return $data;
    }


}